/**
 * 
 */
package com.axxc.jpa.specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.axxc.jpa.entity.StudentEntity;
import com.axxc.jpa.out.model.Student;

/**
 * @ClassName: StudentFilter
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 熊小川
 * @date 2018年5月13日 下午2:57:17
 */
public class StudentSpec implements Specification<StudentEntity> {

	private Student student;

	public StudentSpec(Student student) {
		super();
		this.student = student;
	}

	@Override
	public Predicate toPredicate(Root<StudentEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		// TODO 筛选条件

		Predicate predicate = null;
		if (student != null) {
			predicate = cb.conjunction();

			if (StringUtils.isNotBlank(student.getName())) {
				predicate = cb.and(predicate, cb.like(root.get("name"), "%" + student.getName() + "%"));
			}

		}
		return predicate;
	}

}
